// pages/user/user.js
import {
    Address
} from './address-model.js';

const app = getApp()
const $address = new Address()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: {},
        dialogShow: false,
        threeArea: {
            provice: null,
            city: null,
            area: null
        },
        hasCity: true,
        hasArea: true,
        editAddress: null,
        editRegion: [],
        addressList: null,
        isShowPopup: false,
        tips: '',
        toggleWord: '新增',
        editId: '',
        loadDone: false,
        editStatusReset: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        let that = this
        wx.showLoading({
            title: '加载中',
        })

        let provice = {
            "data": {
                "pid": "0"
            }
        }

        // 获取省一级 pid 0
        $address.getThree(provice, res => {
            if (res.status) {
                that.setData({
                    'threeArea.provice': res.data
                })
            } else {
                wx.hideLoading()
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })
        // 获取地址列表
        $address.getUserAddress({
            data: {
                // "userId": (this.data.userInfo.id).toString()
            }
        }, res => {
            let status = res.status;
            let data = res.data;
            if (status) {
                that.setData({
                    addressList: data,
                    loadDone: true
                })
                wx.hideLoading()
            } else {
                wx.hideLoading()
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })

            }
        })
    },
    preventD(e) {
        return;
    },
    // 设置默认地址
    setDefault(e) {
        let id = e.currentTarget.dataset.id
        let that = this
        let params = {
            data: {
                "id": "" + id,
                "defaultOptions": "1"
            }
        }
        console.log(params)
        this.commonUpdate(params, '确认设置为默认地址吗？', '设置成功')
    },
    //   编辑地址
    edit(e) {
        let province, city, area;
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/pages/editAddress/editAddress?id=' + id,
        })

    },
    //   删除地址
    delete(e) {
        let id = e.currentTarget.dataset.id
        let that = this
        let params = {
            data: {
                "id": "" + id,
                "status": "1"
            }
        }
        console.log(params)
        this.commonUpdate(params, '确定删除吗？', '删除成功')
    },
    commonUpdate(params, content, title) {
        var that = this
        wx.showModal({
            title: '提示',
            content: content,
            success: function (res) {
                if (res.confirm) {
                    $address.updateUserAddress(params, res2 => {
                        console.log(res2)
                        let status = res2.status
                        if (status) {
                            wx.showToast({
                                title: title,
                                icon: 'success',
                                duration: 2000,
                                success() {
                                    setTimeout(() => {
                                        that.onLoad()
                                    }, 2000)
                                }
                            })
                        } else {
                            wx.hideLoading()
                            wx.showToast({
                                title: '服务器错误',
                                icon: 'none'
                            })
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        //this.onLoad()
        $address.getUserAddress({
            data: {}
        }, res => {
            let status = res.status;
            let data = res.data;
            wx.hideLoading()
            wx.stopPullDownRefresh()
            if (status) {
                this.setData({
                    addressList: data,
                })
            } else {
                wx.showToast({
                    title: '服务器错误',
                    icon: 'none'
                })
            }
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})